<template>
  <el-menu
      :default-active="da"
      :default-openeds="defaultActiveArray"
      style="border: none; height: 100%"
      :collapse="isCollapse"
      unique-opened
  >
    <BaseMenuSun
        :isCollapse="isCollapse"
        :menuList="menuList"
        @Clicks="clicks"
    ></BaseMenuSun>
  </el-menu>
</template>

<script>
import BaseMenuSun from "./_base-MenuSun.vue";

export default {
  name: "menus",
  components: {BaseMenuSun},
  props: {
    isCollapse: {
      type: Boolean,
      required: false,
    },
  },
  data() {
    return {
      menuList: [{menuname: "首页", clear: true, id: -9999, path: "/"}],
      da:this.nativeplacestore.get("defaultActive") || "/"
    };
  },
  watch: {
    $route(){
      this.da  = this.nativeplacestore.get("defaultActive") || "/"
    }
  },
  computed: {
    defaultActiveArray() {
      if (!this.nativeplacestore.get("userLogin")) {
        return [];
      }
      let a = this.nativeplacestore
          .get("userLogin")['mr']
          .filter((v) => v.path == this.nativeplacestore.get("defaultActive"));
      if (a && a.length > 0) {
        let pd = this.nativeplacestore
            .get("userLogin")['mr']
            .filter((v) => v.menucode == a[0]["parentcode"]);
        return pd && pd.length > 0 ? [pd[0]["path"]] : [];
      }
      return [];
    },
  },
  created() {
    this.gettreeGet();
  },
  methods: {
    async gettreeGet() {
      if(this.nativeplacestore.get("userLogin")['mc']){
        this.menuList =  [{menuname: "首页", id: -9999, path: "/"},...this.nativeplacestore.get("userLogin")['mc']]
      }else{
        this.menuList =  [{menuname: "首页", id: -9999, path: "/"}]
      }
    },
    clicks(item) {
      if (item.islink == 1) {
        // 是否外链
        window.open(item.url);
      } else {
        this.nativeplacestore.set("defaultActive", item.path);
        let arr = this.nativeplacestore.get("routerTags")
        if(arr){
          if(arr.filter((v)=> v.path==item.path).length>0){

          }else{
            this.nativeplacestore.set("routerTags",[...arr,item]);
          }
        }
        this.$router.push(item.path);
      }
    },
  },
}
</script>

<style scoped>

</style>